﻿using System.Collections;
using System.Collections.Generic;
using Gameplay.PVE;
using Gameplay.PVE.Utils;
using UnityEditor;
using UnityEngine;

public class EnemyAttributes : MonoBehaviour
{
    public int id;
    public int stageId;
    public int spawnType;
    public float spawnParam;

    public float hatredDistance;
    
    private GameObject model;

    private GameObject hatredRange;
    public void UpdateModel()
    {
        for (int i = 0; i < transform.childCount; i++)
        {
            var transform = this.transform.GetChild(i);
            GameObject.DestroyImmediate(transform.gameObject);
        }
        model = GetHeroModelById(id);
        model.transform.SetParent(this.transform);
        model.transform.localPosition = Vector3.zero;
        model.transform.localEulerAngles = Vector3.zero;
        if (hatredRange == null)
        {
            hatredRange = GameObject.CreatePrimitive(PrimitiveType.Cylinder);
            hatredRange.transform.SetParent(transform);
            hatredRange.transform.localPosition = Vector3.zero;
        }
        hatredRange.transform.localScale = new Vector3(hatredDistance * 2, 0.1f, hatredDistance * 2);
    }

    private GameObject GetHeroModelById(int id)
    {
#if UNITY_EDITOR
        var config = PveUtils.GetHeroConfig(id);
        var path = PveResourceManager.GetHeroPath(config.modelId) + ".prefab";
        var obj = AssetDatabase.LoadAssetAtPath<GameObject>(path);
        if (obj == null)
        {
            return new GameObject();
        }
        return Instantiate(obj);
#endif
        return null;
    }
}
